package com.zxh.blog.web.excel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zxh.blog.util.excelDemo.ExcelDemo;
import com.zxh.blog.util.excelDemo.UserDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author:wb-zxh410423
 * @Description:
 * @Date: Created in 10:06 2018/12/10
 */
@Controller
@RequestMapping("excel")
public class ExcelDemoController {

	@RequestMapping(value = "/demo",method = RequestMethod.GET)
	public void getSystemExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String fileName = "attachment" + "_" + new SimpleDateFormat("yyyyMMdd").format(new Date())+".xlsx";
		response.reset();
		response.setHeader("Content-disposition", "attachment; filename="+ fileName);
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
		response.setCharacterEncoding("utf-8");

		String excelData = request.getParameter("excelData");
		List<UserDTO> userDTOS = JSON.parseArray(excelData, UserDTO.class);

//		JSONArray jsonArray = JSON.parseArray(excelData);
//		for(int i = 0;i < jsonArray.size();i++){
//			UserDTO userDTO = new UserDTO();
//			JSONObject jsonObject = jsonArray.getJSONObject(i);
//			userDTO.setId(jsonObject.getString("id"));
//			userDTO.setCreate_time(jsonObject.getString("create_time"));
//			userDTO.setLast_login_time(jsonObject.getString("last_login_time"));
//			userDTO.setNickname(jsonObject.getString("nickname"));
//			userDTO.setStatus(jsonObject.getString("status"));
//			userDTO.setTitle(jsonObject.getString("title"));
//			userDTO.setUpdate_time(jsonObject.getString("update_time"));
//			userDTO.setUsername(jsonObject.getString("username"));
//		}
		ServletOutputStream outputStream = response.getOutputStream();

		ExcelDemo.excelWrite(outputStream,userDTOS,UserDTO.class);
	}
}
